#include "wds_lifeobject.h"

WDSLifeObject::WDSLifeObject() : m_IsAlive(true),
								 m_Life(1.0f),
								 m_MaxLife(1.0f)
{
}


void	WDSLifeObject::ReSpawn()
{
	m_Life = m_MaxLife;
	m_IsAlive = true;
}

void	WDSLifeObject::Tick(float dt)
{
	if(IsAlive())
		m_Facing.Tick(dt);		

	if(!IsReadyToSpawn())		
	{
		m_Movement.Tick(dt);	
		m_Sprite.Tick(dt);
	}

}

void	WDSLifeObject::Render(const math::Vec2& Offset,float Scale)
{
	if(IsInsideScreen() == false)
		return;

	if(IsReadyToSpawn())
		return;

	m_Sprite.m_Pos =  m_Movement.GetPos() - Offset;
	m_Sprite.m_Size = m_Sprite.m_SizeOriginal * Scale;
	m_Sprite.m_Rotation = m_Facing.GetAngle();
	m_Sprite.RenderRotated();

}

bool	WDSLifeObject::IsInsideScreen() const
{
	const math::Vec2 HalfSize = m_Sprite.m_SizeOriginal * 0.5f;
	//return math::Intersection::AABB2D( m_Pos - HalfSize, m_Pos + HalfSize , math::Vec2(0,0) , math::Vec2(1.0f,1.0f) ); 
	return true;
}

void	WDSLifeObject::GotDamaged(float Amount,WDSBullet* pBullet, bool Force)
{
	if(!m_IsAlive)
		return;

	m_Life -= Amount;
	if(m_Life <= 0)
	{
		m_IsAlive = false;
		OnDie();
	}
}
